from login import use_default_login
from collect import CollectThread
from generate import ReportGenerator
import utils


# 生成数据并导出表格
def collect_and_generate(date=utils.get_yesterday(), make_type='all', thread=8):
    library = utils.default_library()
    service = use_default_login()
    account_chunks = library.get_account_chunks(thread)
    chunk_count = 1
    print('获取并生成{}的报表'.format(date))
    for chunk in account_chunks:
        print('=第{}批线程开始='.format(chunk_count))
        pool = []
        for gh_id in chunk:
            account = library.get_account_data(gh_id)
            thread = CollectThread(service, account, date)
            thread.start()
            pool.append(thread)
        for thread in pool:
            thread.join()
        print('=第{}/{}批线程结束='.format(chunk_count, len(account_chunks)))
        chunk_count += 1
    print('=生成报告=')
    if make_type == 'ad':
        reporter = ReportGenerator(library, utils.get_today())
        reporter.make_today_ad()    
    else:
        reporter = ReportGenerator(library, date)
        reporter.make_all()
    input('=运行结束，按回车键退出=')


if __name__ == '__main__':
    yesterday = utils.get_yesterday()
    date = input('请输入收集日期,默认为昨天({}): '.format(yesterday))
    collect_and_generate(date if date else yesterday)
